package com.huawei.hms.framework.network.restclient.hwhttp;

import com.huawei.hms.framework.network.restclient.hianalytics.BaseRequestFinishedInfo;
import com.huawei.hms.framework.network.restclient.hianalytics.RCEventListener;
import com.huawei.hms.framework.network.restclient.hianalytics.RequestFinishedInfo;
import com.huawei.hms.framework.network.restclient.hwhttp.okhttp.MultiHostChangeInterceptor;
import com.huawei.hms.framework.network.restclient.hwhttp.route.RouteInterceptor;
import com.huawei.hms.framework.network.restclient.hwhttp.timeout.AsyncTimeout;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;

public class BuildInSubmit implements Submit {
    private static final String TAG = "BuildInSubmit";
    private final AsyncTimeout asyncTimeout = new AsyncTimeout() {
        public void timedOut() {
            BuildInSubmit.this.cancel();
        }
    };
    private HttpClient client;
    @GuardedBy("this")
    private boolean executed;
    private RCEventListener rcEventListener;
    private Request request;
    private RetryInterceptor retryInterceptor = new RetryInterceptor();

    public BuildInSubmit(HttpClient httpClient, Request request) {
        this.client = httpClient;
        this.request = request;
        this.rcEventListener = httpClient.getEventListenerFactory().create(this);
        this.asyncTimeout.timeout((long) request.getCallTimeout(), TimeUnit.MILLISECONDS);
    }

    public Request request() {
        return this.request;
    }

    public synchronized Response execute() throws IOException {
        Exception exception;
        synchronized (this) {
            if (this.executed) {
                throw new IllegalStateException("Already Executed");
            }
            this.executed = true;
        }
        this.asyncTimeout.enter();
        this.rcEventListener.callStart();
        try {
            Response responseWithInterceptorChain = getResponseWithInterceptorChain();
            this.rcEventListener.callEnd(responseWithInterceptorChain);
            this.asyncTimeout.exit();
            return responseWithInterceptorChain;
        } catch (Exception e) {
            exception = e;
            e = timeoutExit(exception);
            this.rcEventListener.callFailed(e);
            Exception e2;
            if (e2 instanceof InterruptedIOException) {
                throw ((InterruptedIOException) e2);
            }
            throw exception;
        }
    }

    public void enqueue(Callback callback) {
        synchronized (this) {
            if (this.executed) {
                throw new IllegalStateException("Already Executed");
            }
            this.executed = true;
        }
        this.rcEventListener.callStart();
        if (callback == null) {
            throw new NullPointerException("ResultCallback cannot be null");
        }
        RequestThreadPoolManager.getInstance().execute(new AsyncSubmit(this, callback));
    }

    public void cancel() {
        this.rcEventListener.cancel();
        this.retryInterceptor.cancel();
    }

    public synchronized boolean isExecuted() {
        return this.executed;
    }

    public boolean isCanceled() {
        return this.retryInterceptor.isCanceled();
    }

    public Submit clone() {
        return null;
    }

    private Response getResponseWithInterceptorChain() throws IOException {
        RequestFinishedInfo requestFinishedInfo;
        if (this.client.getTrustManager() == null || this.client.getSslSocketFactory() == null) {
            throw new IOException("The trustManager or sslSocketFactory of httpClient is null");
        }
        this.rcEventListener.acquireRequestStart();
        Request request = request();
        this.rcEventListener.acquireRequestEnd(request);
        if (isCanceled()) {
            throw new IOException("Canceled");
        }
        List arrayList = new ArrayList(this.client.getInterceptors());
        arrayList.add(this.retryInterceptor);
        arrayList.add(new RouteInterceptor());
        arrayList.add(new MultiHostChangeInterceptor());
        arrayList.addAll(this.client.getNetworkInterceptors());
        arrayList.add(new CallServerInterceptor());
        try {
            Response proceed = new RealInterceptorChain(this.client, request, arrayList, this.rcEventListener, 0, null).proceed(request);
            requestFinishedInfo = this.retryInterceptor.getRequestTask().getRequestFinishedInfo();
            if (requestFinishedInfo instanceof BaseRequestFinishedInfo) {
                ((BaseRequestFinishedInfo) requestFinishedInfo).setResponse(proceed);
            }
            this.request.getRequestExtraInfo().setRequestFinishedInfo(requestFinishedInfo);
            return proceed;
        } catch (Exception e) {
            Exception exception = e;
            requestFinishedInfo = this.retryInterceptor.getRequestTask().getRequestFinishedInfo();
            if (requestFinishedInfo instanceof BaseRequestFinishedInfo) {
                ((BaseRequestFinishedInfo) requestFinishedInfo).setException(exception);
            }
            this.request.getRequestExtraInfo().setRequestFinishedInfo(requestFinishedInfo);
            throw exception;
        }
    }

    @Nullable
    Exception timeoutExit(@Nullable Exception exception) {
        if (!this.asyncTimeout.exit()) {
            return exception;
        }
        Exception interruptedIOException = new InterruptedIOException("timeout");
        if (exception != null) {
            interruptedIOException.initCause(exception);
        }
        return interruptedIOException;
    }
}
